# -*- coding:utf-8 -*-
__author__: 'Rvelamen'
__data__ = '2022/3/18 16:59'

from typing import Any
from datetime import datetime
import uuid

from sqlalchemy.ext.declarative import as_declarative, declared_attr
from sqlalchemy import Column, String, func, TIMESTAMP, BOOLEAN


class BaseModelMixin(object):
    """ 为所有Model提供公用方法 """

    @declared_attr
    def id(self):
        return Column(String(32), primary_key=True, default=uuid.uuid4().hex)  # 唯一性的UUID

    @declared_attr
    def create_time(self):
        return Column(TIMESTAMP(True), server_default=func.now(), comment='创建时间')

    @declared_attr
    def update_time(self):
        return Column(TIMESTAMP(True), server_default=func.now(), onupdate=func.now(), comment='更新时间')

    @declared_attr
    def is_del(self):
        return Column(BOOLEAN, comment='创建时间')


@as_declarative()
class BaseModel(BaseModelMixin):
    id: Any
    __name__: str

    # 设置表的字符集
    __table_args__ = {"mysql_charset": "utf8"}

    # # 将类名小写并转化为表名 __tablename__
    @declared_attr
    def __tablename__(cls) -> str:
        print(cls.__dict__)
        return cls.__name__.lower()
